package com.gopay.bis.ipay.ebank.operation;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.ipay.ebank.util.PlainTextUtil;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.ipay.ebank.IpayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpTransport;
import com.gopay.remote.bank.ICertHelper;

/**
 * 民生i支付退款接口
 * 
 * @IpayRefund.java
 * @author zhengxue
 * @2016-7-25 下午5:00:22 www.gopay.com.cn Inc.All rights reserved.
 */
public class IpayRefund {
    // 签名验签处理类
    private ICertHelper ipayCertHelper;
    private String refundUrl;

    protected Logger log = LoggerFactory.getLogger(IpayRefund.class);

    public Object refund(IpayMessage message) throws GopayException {
        try {
            String signMessage = (String) ipayCertHelper.sign(message);
            Map<String, String> refundReq = new HashMap<String, String>();
            refundReq.put("oid_partner", message.getOidPartner());
            refundReq.put("no_order", message.getNoOrder());
            refundReq.put("dt_order", message.getDtOrder());
            refundReq.put("money_order", message.getMoneyOrder());
            refundReq.put("ori_no_order", message.getOriNoOrder());
            refundReq.put("notify_url", message.getNotifyUrl());
            refundReq.put("sign", signMessage);
            log.error("IpayRefund req: " + refundReq);
            // 通讯类
            HttpTransport httpTransport = new HttpTransport();
            httpTransport.setUrl(refundUrl);
            Object obj = (Object) httpTransport.submit(refundReq);
            log.error("IpayRefund res: " + refundReq);
            // 解析查询结果
            return parseRefunddRes(obj, message);
        } catch (Exception e) {
            BankLogUtils.printRefundExceptionLog(message, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6087);
        }
    }

    /**
     * 退款结果解析
     * 
     * @param object
     * @return
     * @throws GopayException
     */
    public IpayMessage parseRefunddRes(Object object, IpayMessage refund) throws GopayException {
        try {
            JSONObject jsonObject = JSONObject.fromObject((String) object);
            String sign = jsonObject.getString("sign");
            String moneyOrder = jsonObject.getString("money_order");
            String noOrder = jsonObject.getString("no_order");
            String respCode = jsonObject.getString("resp_code");
            String respMsg = jsonObject.getString("resp_msg");
            String respType = jsonObject.getString("resp_type");
            // 组装退款验签明文
            StringBuffer sb = new StringBuffer();
            sb.append("money_order=").append(moneyOrder).append("&");
            sb.append("no_order=").append(noOrder).append("&");
            sb.append("oid_partner=").append(jsonObject.getString("oid_partner")).append("&");
            sb.append("oid_paybill=").append(jsonObject.getString("oid_paybill")).append("&");
            sb.append("oid_no_order=").append(jsonObject.getString("ori_no_order")).append("&");
            sb.append("pay_type=").append(jsonObject.getString("pay_type")).append("&");
            sb.append("resp_code=").append(respCode).append("&");
            sb.append("resp_msg=").append(respMsg).append("&");
            sb.append("resp_type=").append(respType).append("&");
            sb.append("settle_date=").append(jsonObject.getString("settle_date"));
            String plain = sb.toString();

            refund.setPlainMessage(PlainTextUtil.getPlainText(jsonObject));
            refund.setSignMessage(sign);
            boolean falg = (Boolean) ipayCertHelper.verify(refund); // 验签结果
            refund.setFlag(falg);
            if (falg) {
                refund.setMoneyOrder(moneyOrder);
                refund.setNoOrder(noOrder);
                refund.setRespCode(respCode);
                refund.setRespMsg(respMsg);
                refund.setRespType(respType);
            }
            log.error("IpayRefund parse return: " + ToStringBuilder.reflectionToString(refund));
            return refund;
        } catch (Exception e) {
            BankLogUtils.printRefundExceptionLog(refund, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6088);
        }
    }

    public void setIpayCertHelper(ICertHelper ipayCertHelper) {
        this.ipayCertHelper = ipayCertHelper;
    }

    public void setRefundUrl(String refundUrl) {
        this.refundUrl = refundUrl;
    }

}
